﻿using AMAP.Areas.Supplier.Models.Offer;
using AMAP.Domain;
using AMAP.ServiceInterfaces;
using System.Web.Mvc;

namespace AMAP.Areas.Supplier.Controllers
{
    public class OfferController : Controller
    {
        private IOfferService OfferService;
        private IProductService ProductService;
        private ISupplierService SupplierService;

        public OfferController(IOfferService OfferService, IProductService ProductService, ISupplierService SupplierService)
        {
            this.OfferService = OfferService;
            this.ProductService = ProductService;
            this.SupplierService = SupplierService;
        }


        //
        // GET: /Supplier/Offer/

        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = "Supplier")]
        public ActionResult Create()
        {
            SupplierOfferCreateViewModel Model = new SupplierOfferCreateViewModel();
            Model.FruitList = new SelectList(ProductService.GetAllFruits(), "Id", "Name");
            Model.VegetableList = new SelectList(ProductService.GetAllVegetables(), "Id", "Name");
            Model.OtherList = new SelectList(ProductService.GetAllOthers(), "Id", "Name");

            return View(Model);
        }

        [HttpPost]
        [Authorize(Roles = "Supplier")]
        public ActionResult Create(SupplierOfferCreateViewModel Model)
        {
            foreach (OfferItem Offer in Model.ItemList)
                this.OfferService.AddWithoutSaving(new Offer
                {
                    Unit = Offer.Unit,
                    Price = Offer.Price,
                    Quantity = Offer.Quantity,
                    HarvestDate = Offer.HarvestDate,
                    Supplier = this.SupplierService.GetByEmail(User.Identity.Name),
                    Product = this.ProductService.GetById(Offer.ProductId)
                });

            return RedirectToRoute("Supplier_default", new { controller = "Home", action = "Index" });
        }

    }
}
